﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using System.Data;

namespace HY.WEB.SA
{
    public partial class zj_add : PageBase
    {
        public string us, um, ubh;
        string tab = "zj";
        protected void Page_Load(object sender, EventArgs e)
        {

            us = Session["ID"].ToString();
            um = Session["user"].ToString();
            ubh = Session["bh"].ToString();


            if (!IsPostBack)
            {
                if (!String.IsNullOrEmpty(Request["stype"]))
                {
                     hidstype.Text = Request["sid"].Trim();                
                }


                //业务级别
               DataTable te = BLL.Bllbase.get_table("[sys_level]");
                ddllevel.Items.Clear();
                for (int i = 0; i < te.Rows.Count; i++)
                {
                    ddllevel.Items.Add(new ExtAspNet.ListItem(te.Rows[i]["name"].ToString(), te.Rows[i]["name"].ToString()));
                }
                ddllevel.DataBind();

                ExtAspNet.PageContext.RegisterStartupScript
                          (
                          "var d=Ext.getCmp('" + ddlzj.ClientID + "');" +
                          " var oldInput=\"\";" +
                          " d.listWidth=500; " +
                             " d.addListener(\"specialkey\",function(f,e){ " +  //keyup
                          " var newInput=document.getElementById(\"" + ddlzj.ClientID + "\").value;" +
                           " if (oldInput==newInput) {return;} " +
                          " oldInput=newInput;" +
                           " __doPostBack('loaddata',newInput);" +
                           "});"
                          );

            }
            else
            {

                if (Request["__EVENTTARGET"] == "loaddata")
                {

                    string sqltxt = "SELECT * FROM zj where charindex('" +
                        Request["__EVENTARGUMENT"].ToString() + "',zname)>0 ";
                    zname.Text = Request["__EVENTARGUMENT"].ToString();

                    DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
                    ddlzj.Items.Clear();
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        ddlzj.Items.Add(new ExtAspNet.ListItem(dt.Rows[i]["zname"].ToString(), dt.Rows[i]["zname"].ToString()));
                    }
                    //下面这句话是有道理的，extjs默认效果在autocomplete之后，值为变成下拉框的第一项，有点破坏效果，这里纠正过来，可以输入多个关键字  
                    ExtAspNet.PageContext.RegisterStartupScript("document.getElementById(\"" + ddlzj.ClientID + "\").value='" +
                             Request["__EVENTARGUMENT"] + "';");

                }


            }      
        }

        protected void btnctel_Click(object sender, EventArgs e)
        {
            if (tel.Text != "")
                if (BLL.Bllbase.check_exits("company", "ctel='" + tel.Text.Trim() + "'"))
                    lbltel.Text = "电话已经存在，请检查信息是否重复。";
                else
                    lbltel.Text = tel.Text + "可以使用";
        }
                

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Model.SC.zhongjie jb = new Model.SC.zhongjie();

          jb.zbh = BLL.BllAutoCode.GetMaxBH("zj"); //岗位编号
          if (jb.zbh == "") return;

          jb.zname = zname.Text;

          if (zname.Text != "")
              if (BLL.Bllbase.check_exits("zj", "zname='" + zname.Text.Trim() + "'"))
              {
                  Alert.Show("中介名已经存在，请检查信息是否重复。"); return;
              }

          jb.zaddress = zaddress.Text;
          jb.memo = memo.Text;
          jb.area = area.Text;
          jb.person = person.Text;

          if (tel.Text != "")
              if (BLL.Bllbase.check_exits("zj", "tel='" + tel.Text.Trim() + "'"))
              {
                  Alert.Show("电话已经存在，请检查信息是否重复。"); return;
              }

          jb.tel = tel.Text;
          jb.fax = fax.Text;
          jb.qq = QQ.Text;
          jb.email = email.Text;
          jb.mtel = mtel.Text;
          jb.msn=msn.Text;
          jb.pjob = txtjob.Text;
          jb.psex = txtsex.Text;
          jb.psize = txtsize.Text;
          
          jb.slevel = ddllevel.SelectedValue;

          BLL.SC.zhongjie sco = new BLL.SC.zhongjie();
             sco.Add(jb);


             //更新业务员操作记录
             BLL.Bllbase.update_rec(tab, um, " zbh='" + jb.zbh + "' ");

             ////写入收藏表
             Alert.Show(BLL.BllSales.intosales("中介", jb.zbh, jb.zname, ubh, um,"ZJ"));
            
            Alert.Show("添加完成 ");
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

       
    }
}